package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    private TeacherService teacherService = new TeacherService();
    private Scanner sc = new Scanner(System.in);

    public void start() {
        //键盘录入数据
        //给出老师系统的表单
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            //创建选择语句
            switch (choice) {
                case "1":
                    //System.out.println("添加");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除");
                    deleteTeacher();
                    break;
                case "3":
                    //System.out.println("修改");
                    updateTeacher();
                    break;
                case "4":
                    //System.out.println("查看");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢您使用<老师> 管理系统");
                    return;
                default:
                    System.out.println("您输入的数据有误,请重新输入");
            }
        }

    }

    //添加老师
    public void addTeacher() {
        //让用户输入老师学号
        System.out.println("请输入工号");
        String id;
        while (true) {
            id = sc.next();
            boolean flag = teacherService.isExists(id);
            if (flag) {
                System.out.println("请重新输入工号");
            } else {
                break;
            }
        }
        Teacher teacher = inputTeacherInfo(id);

        //调用teacherService业务员对象添加学生
        boolean flag = teacherService.addTeacher(teacher);
        if (flag) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    //删除老师信息
    public void deleteTeacher() {
        //先判断库中是否有元素,没有即返回并给出提示信息
        Teacher[] teacherArray = teacherService.findAllTeacher();
        if (teacherArray == null) {
            System.out.println("查无信息,请先添加信息");
            return;
        }
        String delId = inputTeacherId();
        teacherService.deleteTeacher(delId);
        System.out.println("删除成功");
    }

    //修改老师工号
    public void updateTeacher() {
        //先判断库中是否有元素,没有即返回并给出提示信息
        Teacher[] teacherArray = teacherService.findAllTeacher();
        if (teacherArray == null) {
            System.out.println("查无信息,请先添加信息");
            return;
        }
        String updateId = inputTeacherId();
        Teacher teacher = inputTeacherInfo(updateId);
        //调用业务员的update方法
        teacherService.updateTeacherId(teacher, updateId);
        System.out.println("更新成功");
    }

    //查询老师信息
    public void findAllTeacher() {
        Teacher[] teacherArray = teacherService.findAllTeacher();
        if (teacherArray == null) {
            System.out.println("查无信息,请先添加信息");
            return;
        }
        //不为空即遍历打印数组
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < teacherArray.length; i++) {
            Teacher teacher = teacherArray[i];
            if (teacher != null) {
                System.out.println(teacher.getId() + "\t" + teacher.getName() + "\t" + teacher.getAge() + "\t\t" + teacher.getBirthday());
            }
        }

    }

    //键盘录入老师工号
    public String inputTeacherId() {
        System.out.println("请输入老师工号");
        String delId;
        //标记位
        boolean flag = false;
        while (true) {
            //1. 键盘录入要删除的学号
            delId = sc.next();
            flag = teacherService.isExists(delId);
            if (flag) {
                break;
            } else {
                System.out.println("您输入的工号不存在,请重新输入");
            }
        }
        return delId;
    }

    //键盘录入老师信息
    public Teacher inputTeacherInfo(String id) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        String age = sc.next();
        System.out.println("请输入生日");
        String birthday = sc.next();

        Teacher teacher = new Teacher();
        teacher.setId(id);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);
        return teacher;
    }
}
